package com.ftdigital.primemovers_operators.fragment;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.TextView;

import com.ftdigital.primemovers_operators.MainActivity;
import com.ftdigital.primemovers_operators.R;
import com.ftdigital.primemovers_operators.config.RequestAddr;
import com.ftdigital.primemovers_operators.util.http.AjaxCallBack;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * 平台简介
 */
@EFragment(R.layout.fragment_platfrom_introduce)
public class PlatfromIntroduceFragment extends BaseFragment {
    private static final String APP_CACAHE_DIRNAME = "/webcache";
    @ViewById(R.id.webview)
    WebView newsWebview;
    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @AfterViews
    @Override
    void initView() {
        backBtn.setVisibility(View.VISIBLE);
        topTitle.setText(getResources().getString(R.string.title_platform));

        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
//        topTitle.setText(R.string.rank_title);
        newsWebview.getSettings().setJavaScriptEnabled(true);
        newsWebview.getSettings().setUseWideViewPort(true);
        newsWebview.getSettings().setLoadWithOverviewMode(true);
        WebSettings webSettings = newsWebview.getSettings();
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

        newsWebview.getSettings().setBuiltInZoomControls(true);
        newsWebview.getSettings().setUseWideViewPort(true);
        newsWebview.getSettings().setAllowFileAccess(true);
//        newsWebview.getSettings().setPluginsEnabled(true);

        newsWebview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        newsWebview.getSettings().setCacheMode(
                WebSettings.LOAD_NO_CACHE); // ���� ����ģʽ
        newsWebview.getSettings().setDomStorageEnabled(true);
        newsWebview.getSettings().setDatabaseEnabled(true);
        String cacheDirPath = MainActivity.getInstance().getFilesDir()
                .getAbsolutePath()
                + APP_CACAHE_DIRNAME;
        newsWebview.getSettings().setDatabasePath(cacheDirPath);
        newsWebview.getSettings().setAppCachePath(cacheDirPath);
        newsWebview.getSettings().setAppCacheEnabled(true);
        newsWebview.getSettings().setTextZoom(250);

        // newsWebview.setBarHeight(3);
        newsWebview.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        newsWebview.setWebChromeClient(new WebChromeClient() {

            public void openFileChooser(ValueCallback<Uri> uploadMsg,
                                        String acceptType, String capture) {
                MainActivity.getInstance().mUploadMessage = uploadMsg;
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
                getActivity().startActivityForResult(
                        Intent.createChooser(intent, ""),
                        MainActivity.getInstance().FILECHOOSER_RESULTCODE);

            }

            // 3.0 + �����������
            public void openFileChooser(ValueCallback<Uri> uploadMsg,
                                        String acceptType) {
                MainActivity.getInstance().mUploadMessage = uploadMsg;
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
                getActivity().startActivityForResult(
                        Intent.createChooser(intent, "��ɲ�����Ҫʹ��"),
                        MainActivity.getInstance().FILECHOOSER_RESULTCODE);
            }

            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                MainActivity.getInstance().mUploadMessage = uploadMsg;
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
                getActivity().startActivityForResult(
                        Intent.createChooser(intent, "��ɲ�����Ҫʹ��"),
                        MainActivity.getInstance().FILECHOOSER_RESULTCODE);

            }

        });
        showWeb();
    }

    private void showWeb() {
        newsWebview.loadUrl(RequestAddr.webUrl);
        xtHttp.get(RequestAddr.webUrl, new AjaxCallBack<Object>() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    @Click(R.id.back_btn)
    @Override
    void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStack();
                break;
        }
    }

}
